{-# OPTIONS_GHC -fglasgow-exts #-}
module Hixay.UI.MainFrame where

import Graphics.UI.WX
import Hixay.UI.GenerateScript
import Hixay.UI.Tools
import Control.Monad

mainFrame :: IO (Frame ())
mainFrame = do -- the application frame
    wspVar <- varCreate ["","","xi_wsp_proxy.x04",""]
    f   <- frame [text := "Hixay! Tools for working with SAP XI Directory"]
    notebook f [visible := False]
    s <- splitterWindow f []
    pLeft <- panel s []
    pRight <- panel s []

    ps <- mapM ((panel pRight [visible := False] >>=) . snd) $ actions wspVar
    ch <- singleListBox pLeft [
            items := map fst $ actions wspVar, 
            bgcolor := colorSystem ColorInfoBk,
            on select ::= \w -> get w selection >>= setVisibility ps >>= 
                    \p -> (windowSetLayout pRight . margin 10 . fill . widget) p 
                    -- I didn't find another way to repaint correctly
                    >> refresh_ s 
        ]
    windowSetLayout f $ 
            fill  $ vsplit s 0 160 (container pLeft $ margin 10 $ vfill $ widget ch) (container pRight glue)
    set f [size := Size 640 480]
    return f
    where
        setVisibility ps n = zipWithM_ (\a b -> set a [visible := (b == n)]) ps [0..] >> return (ps!!n)
        actions wspVar = [
                ("Connection parameters", connectionParams wspVar), 
                ("Generate script", generateScript wspVar), 
                ("About", about)
            ]

connectionParams :: forall a. Var [String] -> Window a -> IO (Window a)            
connectionParams wspVar p = do
    te <- zipWithM (\n t -> textEntry p [alignment := AlignLeft, color := blue, text := t] >>= 
            \x -> set x [on idle := set_color x n] >> return x) [(0::Int)..3] ["","","xi_wsp_proxy.x04",""]
    ok <- button p [text := "Apply Params" , on command := mapM (flip get text) te >>= varSet wspVar >> 
            mapM_ (\x -> set x [color := black] >> refresh x) te]
    windowSetLayout p $ column 25 [
            boxed "Connection parameters" (margin 20 $ grid 50 10 $ zipWith mkRow
                    ["Server", "Authentication", "File proxy", "Log file"] te),
            floatTopLeft $ widget ok
        ]
    return p
    where
        set_color x n = do
            old <- liftM (!!n) (varGet wspVar)
            cur <- get x text
            oldcolor <- get x color
            if (oldcolor == black && old == cur) || (oldcolor == blue && old /= cur) 
                then return True
                else set x [color := if old == cur then black else blue] >> repaint x >> return True
        
about :: forall a. Window a -> IO (Window a)
about p = do
    set p [fontSize := 14, bgcolor := colorSystem ColorInfoBk]
    windowSetLayout p $ boxed "About Hixay" $ margin 10 $ fill $ label $ unlines [
            "Hixay is a tool for working with SAP XI Directory through web-services Directory API", 
            "", 
            "Copyright by Dmitry Olshansky && Ilya Kuznetsov. 2009-..."
        ]
    return p
